package top.bigdata.mapper;

import com.baomidou.dynamic.datasource.annotation.DS;
import org.apache.ibatis.annotations.*;
import top.bigdata.model.entity.Result;

import java.sql.Timestamp;

@Mapper
@DS("mysql")
public interface ResultMapper {
    @Select("select * from result where id = #{id}")
    Result getResult(String id);

    @Insert("""
    insert into result (id, last_query, data)
    values (#{id}, #{lastQuery}, #{data})
    on duplicate key update
    last_query = values(last_query),
    data = values(data)
    """)
    int addResult(Result result);

    @Update("update result set last_query = #{timestamp} where id = #{id}")
    int updateQueryTime(@Param("id") String id, @Param("timestamp") Timestamp timestamp);

    @Delete("""
            DELETE FROM result
            WHERE id NOT IN (
            SELECT id FROM
            (SELECT id FROM result ORDER BY last_query DESC LIMIT #{limitNum}) AS t)
            """)
    int clearResult(int limitNum);

}
